﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YouHP.Model
{
    public class PrescriptionSearchCondition
    {
        public int PrescriptionId { get; set; }
        public string PrescriptionName { get; set; }
        public int DiseaseId { get; set; }
        public string DiseaseName { get; set; }
        public int DoctorId { get; set; }
        public bool UseIsClassicAsCriterion { get; set; }
        public bool IsClassic { get; set; }
        public bool UseMinScoreAsCriterion { get; set; }
        public double MinScore { get; set; }
        public bool UseMaxScoreAsCriterion { get; set; }
        public double MaxScore { get; set; }

        public PrescriptionSearchCondition()
        {
            PrescriptionId = -1;
            PrescriptionName = DiseaseName = String.Empty;
            DiseaseId = DoctorId = -1;
            UseIsClassicAsCriterion = false;
            IsClassic = false;
            UseMinScoreAsCriterion = false;
            MinScore = double.MaxValue;
            UseMaxScoreAsCriterion = false;
            MaxScore = double.MinValue;
        }

        public string ToSqlConditionString()
        {
            string sql = string.Empty;
            bool firstCondition = true;

            if (PrescriptionId > 0)
            {
                sql = string.Format(" [Prescription].[Id] = {0}", PrescriptionId);
                firstCondition = false;
            }

            if (PrescriptionName.Length > 0)
            {
                if (!firstCondition)
                {
                    sql += " and ";
                }
                sql = string.Format(" [Prescription].[Name] like '%{0}%'", PrescriptionName);
            }

            if (DiseaseId > 0)
            {
                if (!firstCondition)
                {
                    sql += " and ";
                }
                sql += string.Format(" [Prescription].[DiseaseId]={0}", DiseaseId);
            }

            if (DiseaseName.Length > 0)
            {
                if (!firstCondition)
                {
                    sql += " and ";
                }
                sql += string.Format(" [Prescription].[DiseaseName] like '%{0}%'", DiseaseName);
            }

            if (DoctorId > 0)
            {
                if (!firstCondition)
                {
                    sql += " and ";
                }
                sql += string.Format(" [Prescription].[DoctorId]={0}", DoctorId);
            }

            if (UseIsClassicAsCriterion)
            {
                if (!firstCondition)
                {
                    sql += " and ";
                }
                sql += string.Format(" [Prescription].[IsClassic]={0}", IsClassic ? 1 : 0);
            }

            if (UseMinScoreAsCriterion)
            {
                if (!firstCondition)
                {
                    sql += " and ";
                }
                sql += string.Format(" [Prescription].[AverageScore] >= {0}", MinScore);
            }

            if (UseMaxScoreAsCriterion)
            {
                if (!firstCondition)
                {
                    sql += " and ";
                }
                sql += string.Format(" [Prescription].[AverageScore] <= {0}", MaxScore);
            }

            return sql;
        }
    }
}
